import Toast from '@vant/weapp/toast/toast'
import api from '../../api/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginData: {
      loginPhone: '',
      loginPassword: ''
    },
    registerData: {
      registerPhone: '',
      registerPassword: '',
      registerNickName: ''
    },
    show: false
  },
  back() {
    wx.navigateBack()
  },
  toFindPwd() {
    wx.navigateTo({
      url: '/pages/find-password-first/find-password-first',
    })
  },
  async login(e) {
    let phone = e.detail.value.phone
    let password = e.detail.value.password
    if (phone == '') {
      Toast.fail('请输入手机号')
      return
    }
    if (password == '') {
      Toast.fail('请输入密码')
      return
    }

    const res = await api.login({ phone, password })
    if (res.data.code == 200) {
      console.log('登录', res);
      Toast.success(res.data.msg)
      wx.setStorageSync('token', res.data.token)
      setTimeout(() => {
        wx.switchTab({
          url: '/pages/mine/mine',
        })
      }, 2000)
    } else if (res.data.code == 202) {
      Toast.fail(res.data.msg)
    } else if (res.data.code == 201) {
      Toast.fail(res.data.msg)
    }
  },
  showReg() {
    this.setData({
      show: true
    })
  },
  hideReg() {
    this.setData({
      show: false
    })
  },

  async reg(e) {
    let phone = e.detail.value.phone
    let password = e.detail.value.password
    let nickName = e.detail.value.nickName
    const ruleRegPhone = /^1[3-9]\d{9}$/
    const ruleRegPassword = /^[A-Za-z][A-Za-z\d]{5,15}$/
    const ruleRegNickName = /^[A-Za-z\u4e00-\u9fa5]{1,5}$/
    if (phone == '') {
      Toast.fail('请输入手机号')
      return
    }
    if (!ruleRegPhone.test(phone)) {
      Toast.fail('手机号格式错误')
      return
    }
    if (password == '') {
      Toast.fail('请输入密码')
      return
    }
    if (!ruleRegPassword.test(password)) {
      Toast.fail('密码格式错误')
      return
    }
    if (nickName == '') {
      Toast.fail('请输入昵称')
      return
    }
    if (!ruleRegNickName.test(nickName)) {
      Toast.fail('昵称格式错误')
      return
    }
    const res = await api.register({ phone, password, nickName })
    if (res.data.code == 102) {
      Toast.fail('手机号已被占用')
      // registerData.registerPhone = ''
      this.setData({
        ['registerData.registerPhone']: ''
      })
    }
    // 如果注册成功，关闭弹窗
    if (res.data.code == 100) {
      Toast.success('注册成功!')
      this.setData({
        ['loginData.loginPassword']: password,
        ['loginData.loginPhone']: phone,
        // ['loginData.loginPassword']: this.data.registerData.registerPassword,
        // ['loginData.loginPhone']: this.data.registerData.registerPhone,
        show: false,
        ['registerData.registerPhone']: '',
        ['registerData.registerNickName']: '',
        ['registerData.registerPassword']: ''
      })
      // loginData.loginPassword = registerData.registerPassword
      // loginData.loginPhone = registerData.registerPhone
      // 注册成功清空输入框
      // registerData.registerPhone = ''
      // registerData.registerNickName = ''
      // registerData.registerPassword = ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})